import { Provide } from '@midwayjs/decorator';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { CmsCategoryEntity } from '../entity/category';

/**
 * 描述
 */
@Provide()
export class CmsCategoryService extends BaseService {
  @InjectEntityModel(CmsCategoryEntity)
  CmsCategoryEntity: Repository<CmsCategoryEntity>;

  /**
   * 重载删除接口
   */
  async delete(ids: any) {
    for (const id of ids) {
      const category = await this.CmsCategoryEntity.findOne({
        where: { id: id },
      });
      if(category.arttcle_count > 0){
        throw new CoolCommException('无法删除分类文章数量大于0的数据');
      }
    }
    return await super.delete(ids);
  }
}
